<template>
  <div>
    <div class="box">
      <img src="../../assets/image/pic.jpg" />
      <span>账号登录</span>
      <van-cell-group style="border:0">
        <van-row>
          <van-field v-model="username" type='number' @blur="registerUser" placeholder="+86 > 手机号码" />
        </van-row>
      </van-cell-group>
      <van-cell-group>
        <van-row>
          <van-field v-model="password" type="password" placeholder="短信验证码" @blur="getMes">
               <van-button slot="button" size="small" class="send" @click="send">
                   <a href="" >发送验证码</a>
               </van-button>
          </van-field>
        </van-row>
      </van-cell-group>

      <van-button color="#FF6700" size="large" @click="goLogin">立即登录/注册</van-button>
      <van-button class="zhuce" color="#ccc" size="large" @click="passwordLogin">用户名密码登录</van-button>
      <div>
        <span>
          <a href="javascript::">收不到验证码?</a>
        </span>

      </div>
      <van-divider :style="{ color: '#ddd', borderColor: '#ddd', padding: '0 6px' }">其他方式登录</van-divider>
      <div class="loginMethods">
        <a href="javascript::">
          <img class="pic" src="../../assets/image/weibo.jpg" alt />
        </a>
        <a href="javascript::">
          <img src="../../assets/image/zfb.jpg" />
        </a>
        <a href="javascript::">
          <img src="../../assets/image/weixin.jpg" />
        </a>
      </div>
    </div>
  </div>
</template>
<script>
import { mapMutations } from 'vuex'
import { Toast } from 'vant'
export default {
  data () {
    return {
      username: '18384247486',
      password: '',
      msg: ''

    }
  },
  created () {
    this.clearCart()
  },
  methods: {
    ...mapMutations(['clearCart']),
    registerUser () {
      if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(this.username)) {
        Toast('请输入符合规则的11位手机号')
        return false
      }
      return true
    },
    getMes () {
      if (!/^(\w){4,6}$/.test(this.password)) {
        Toast('请输入验证码')
        return false
      }
      return true
    },
    send () {
      if (this.registerUser()) {
        this.$http.get('register', {
          params: {
            msg: this.username
          }
        }).then(res => {
          this.msg = res.data.msg
        }).catch(res => {
          Toast('手机号未注册')
        })
      } else {
        Toast('手机号错误')
      }
    },
    goLogin () {
      if (this.getMes()) {
        if (this.password === this.msg) {
          this.$router.push('home')
          window.localStorage.setItem('token', 'ok')
        } else {
          Toast('验证码输入错误')
        }
      } else if (this.registerUser()) {
        Toast('请输入正确的验证码')
      } else {
        Toast('请输入正确的用户名')
      }
    },
    passwordLogin () {
      this.$router.push('login')
    }
  }
}
</script>
<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
.box {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 80%;
  margin: 65px auto;
  // background-color: pink;
  img {
    width: 60px;
    height: 60px;
    vertical-align: middle;
    border-radius: 10px;
  }
  span {
    font-size: 20px;
    margin: 20px 0;
  }
  van-field {
    margin-bottom: 20px;
    border-bottom: 2px solid #ccc;
  }
  .van-hairline--top-bottom {
    position: static;
  }
  .van-cell-group {
    width: 100%;
    .van-row {
      border-bottom: 2px solid #ccc;
    }
  }
  .van-cell {
    padding-left: 0;
  }

  .van-button {
    margin-top: 20px;
    border-radius: 8px;
    font-size: 16px;
  }
.send{
    margin-top: 0!important;
    border: 0px;
}
  .zhuce {
    color: #000 !important;
    border: 1px solid #ccc;
    background-color: #fff !important;
  }

  a {
    font-size: 14px;
    color: #ccc;
  }
  .van-divider {
    width: 100%;
  }
  .loginMethods {
    a {
      margin: 10px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
      }
    }
  }
}
</style>
